<template>
	<view class="container">
		<view class="page-header" :style="{paddingTop: safeArea.top + 'px', background: isBlack ? '#17161B' : 'transparent'}">
			<view class="back-icon" @click="backPage">
				<image mode="aspectFit" src="../../../static/image/arrow-white.png" class="icon"></image>
			</view>
		</view>
		<image class="page-bg" src="../../../static/image/vip/VIP-bg.png" mode="aspectFit" :style="{top:safeArea.top + 'px'}"></image>
		<view class="page-content">
			<view class="tab-list">
				<view :class="['tab-cell', vipType == index ? 'active' : '']" v-for="(item, index) in vipList" :key="item.id" @click="changeType(index)">
					<view class="title">{{item.name}}</view>
					<image v-if="vipType == index" mode="aspectFit" src="../../../static/image/vip/special-line.png" class="special-line"></image>
				</view>
			</view>
			<view class="card-content">
				<view class="detail-content">
					<view class="detail-title">超值升级尊享6大权益</view>
					<view class="detail-desc">开通会员享多重豪礼</view>
					<view class="detail-list">
						<view class="detail-cell">
							<image mode="aspectFit" src="../../../static/image/vip/vip-01.png" class="icon"></image>
							<view class="detail-tips">送菜次数</view>
							<view class="detail-tips">26次</view>
						</view>
						<view class="detail-cell">
							<image mode="aspectFit" src="../../../static/image/vip/vip-02.png" class="icon"></image>
							<view class="detail-tips">单次配送</view>
							<view class="detail-tips">6斤</view>
						</view>
						<view class="detail-cell">
							<image mode="aspectFit" src="../../../static/image/vip/vip-03.png" class="icon"></image>
							<view class="detail-tips">挑选品种</view>
							<view class="detail-tips">6种</view>
						</view>
						<view class="detail-cell">
							<image mode="aspectFit" src="../../../static/image/vip/vip-04.png" class="icon"></image>
							<view class="detail-tips">园区参观</view>
							<view class="detail-tips">8次</view>
						</view>
						<view class="detail-cell">
							<image mode="aspectFit" src="../../../static/image/vip/vip-05.png" class="icon"></image>
							<view class="detail-tips">会员生日</view>
							<view class="detail-tips">赠品福利</view>
						</view>
						<view class="detail-cell">
							<image mode="aspectFit" src="../../../static/image/vip/vip-06.png" class="icon"></image>
							<view class="detail-tips">免费专享</view>
							<view class="detail-tips">配送</view>
						</view>
					</view>
				</view>
				<view class="btn" @click="submitForm">¥{{vipList[vipType].price}}元 立即开通</view>
				<navigator hover-class="none-hover" class="check-btn" :url="`/pagesB/vip/topUp/interests?id=${vipList[vipType].id}`">
					<view class="check-tips">权益详情</view>
					<u-icon name="arrow-right" color="#796352" size="22"></u-icon>
				</navigator>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				interests: '',
				show: false,
				vipList: [], // 会员类型数据
				orderId: null,
				safeArea: {},
				top: 0,
				vipType: 0,
				isBlack: false
			}
		},
		onLoad() {
			this.getVipList()
		},
		onShow() {
			this.safeArea = uni.getSystemInfoSync().safeArea
		},
		onPageScroll(e) {
			if(e.scrollTop > this.safeArea.top){
				this.isBlack = true
			}else{
				this.isBlack = false
			}
		},
		methods: {
			changeType(index){
				this.vipType = index
			},
			// 查看权益
			showVipInfo(item){
				this.interests = item.interests
				this.show = true
			},
			// 获取列表数据
			getVipList() {
				const that = this
				this.$api.request.getVipList({}, res => {
					if(res.code == 200){
						that.vipList = res.data
					}
				})
			},
			// 选择年卡
			selectVip(index) {
				if(this.vipType != index){
					this.vipType = index
				}else if(this.vipType == index) {
					this.vipType = null
				}
			},
			// 充值
			submitForm() {
				const that = this
				if(this.vipType == null){
					uni.showToast({
						title: '请选择VIP类型',
						icon: 'none'
					})
				}else{
					// 获取雪花id
					this.$api.request.getSnowflakeID({}, res => {
						if(res.code == 200){
							that.orderId = res.data
							if(that.orderId != null){
								this.$api.request.vipTopUpPay({
									// 商品描述
									body: that.vipList[that.vipType].name,
									// 商户订单号
									out_trade_no: that.orderId,
									// 总金额 x 100  以分为单位
									total_fee: that.vipList[that.vipType].price,
									// 用户openid
									openId: uni.getStorageSync('openId')
								}, res => {
									if(res.code == 200){
										this.toPay(res)
									}
								})
							}
						}else {
							uni.showToast({
								title: "网络延迟，请重试",
								icon: "none",
								mask: true,
								duration: 2000
							})
						}
					})

				}
			},
			// 支付
			toPay(res) {
				console.log(res);
				const that = this
				uni.requestPayment({
					timeStamp: res.data.timeStamp,
					nonceStr: res.data.nonceStr,
					package: res.data.package,
					signType: res.data.signType,
					paySign: res.data.paySign,
					success: (res) => {
						console.log("支付成功", res)
						if (res.errMsg == "requestPayment:ok") {
							this.$api.request.addVipMember({
								memberId: uni.getStorageSync('userInfo').id,
								vipTypeId: that.vipList[that.vipType].id,
								topUpOrder: that.orderId,
								price: Number(that.vipList[that.vipType].price),
								deadline: that.vipList[that.vipType].deadline
							}, res => {
								if(res.code == 200){
									uni.showToast({
										title: "支付成功",
										icon: "success",
										mask: true,
										duration: 2000
									})
									that.$api.request.userInfo({
										type: 'application/x-www-form-urlencoded',
									}, res => {
										if(res.code == 200){
											uni.setStorageSync('userInfo', res.data)
											setTimeout(() => {
												uni.navigateBack({
													delta: 1
												})
											}, 1000)
										}
									})
								}
							})
						}
					},
					fail: (err) => {
						console.log(err);
						uni.showToast({
							title: '取消支付',
							icon: 'error'
						})
					}
				})
			
			},
			// 返回
			backPage() {
				uni.navigateBack({
					delta: 1,
					fail() {
						uni.switchTab({
							url: "/pages/sys/home/index"
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container{
		min-height: calc(100vh - 40rpx - constant(safe-area-inset-bottom));
		min-height: calc(100vh - 40rpx - env(safe-area-inset-bottom));
		padding-bottom: calc(40rpx + constant(safe-area-inset-bottom));
		padding-bottom: calc(40rpx + env(safe-area-inset-bottom));
		background: #17161B;
		position: relative;
		.page-header{
			position: sticky;
			top: 0;
			left: 0;
			right: 0;
			height: 76rpx;
			display: flex;
			align-items: center;
			transition: .2s;
			z-index: 99;
			.back-icon{
				width: 64rpx;
				height: 100%;
				padding-left: 20rpx;
				display: flex;
				align-items: center;
				.icon{
					width: 32rpx;
					height: 32rpx;
				}
			}
		}
		.page-bg{
			position: absolute;
			right: 24rpx;
			width: 556rpx;
			height: 252rpx;
		}
		.page-content{
			padding: 54rpx 34rpx constant(safe-area-inset-bottom);
			padding: 54rpx 34rpx env(safe-area-inset-bottom);
			position: relative;
			z-index: 10;
			.tab-list{
				display: flex;
				align-items: stretch;
				justify-content: center;
				.tab-cell{
					margin-right: 68rpx;
					position: relative;
					&:last-child{
						margin: 0;
					}
					.title{
						font-size: 40rpx;
						background: linear-gradient(180deg, #F0D9BE 0%, #DCB18A 100%);
						background-clip: text;
						color: transparent;
						font-style: italic;
						font-weight: bold;
						opacity: 0.5;
					}
					.special-line{
						position: absolute;
						width: 60rpx;
						height: 20rpx;
						left: 0;
						right: 0;
						margin: 16rpx auto 0;
						display: block;
					}
				}
				.active{
					.title{
						opacity: 1;
					}
				}
			}
			.card-content{
				position: relative;
				padding: 86rpx 34rpx;
				border-radius: 16rpx;
				background-color: #17161B;
				background-clip: padding-box;
				margin-top: 54rpx;
				&::after{
				  content: '';
				  position: absolute;
				  top: 0;
				  right: 0;
				  left: 0;
				  bottom: 0;
				  z-index: -1;
				  margin: -1.4px;
				  border-radius: inherit;
				  background: linear-gradient(45deg, #A59784, #332B24);
				}
				.detail-title{
					text-align: center;
					font-size: 40rpx;
					color: #DCB18A;
				}
				.detail-desc{
					text-align: center;
					font-size: 26rpx;
					color: #796352;
					padding-top: 20rpx;
				}
				.detail-list{
					display: flex;
					align-items: stretch;
					flex-wrap: wrap;
				}
				.detail-cell{
					width: 33.333%;
					padding-top: 48rpx;
					.icon{
						width: 88rpx;
						height: 88rpx;
						margin: 0 auto 20rpx;
						display: block;
					}
					.detail-tips{
						text-align: center;
						font-size: 24rpx;
						color: #DCB18A;
					}
				}
				.btn{
					margin: 90rpx auto 0;
					height: 92rpx;
					border-radius: 46rpx;
					font-size: 32rpx;
					text-align: center;
					display: flex;
					align-items: center;
					justify-content: center;
					background: linear-gradient(to right, #FEE2C6, #F0BA8A);
					color: #4F2600;
				}
				.check-btn{
					display: flex;
					align-items: center;
					margin-top: 40rpx;
					justify-content: center;
					font-size: 26rpx;
					color: #796352;
					.check-tips{
						margin-right: 10rpx;
					}
				}
			}
		}
	}
</style>